package net.bingosoft.common.util;

import net.bingosoft.common.ObjectRegistry;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Jul 22, 2008 7:54:51 PM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:查看数据库连接池的状态的工具,用于检测问题
 * ChangeLog:
 */
public class DatasourceObserver {
	private DatasourceObserver(){}
	
	/**
	 * 查看数据库的运行时连接消耗情况
	 * @return
	 */
	public static String runtimeStatus(){
		BasicDataSource bds = ObjectRegistry.getBean("dataSource", BasicDataSource.class);
		return "Active : " + bds.getNumActive() + " Idle : " + bds.getNumIdle();
	}
	
	/**
	 * 查看数据库的全部状态
	 * @return
	 */
	public static String allStatus(){
		BasicDataSource bds = ObjectRegistry.getBean("datasource", BasicDataSource.class);
		return ToStringBuilder.reflectionToString(bds, ToStringStyle.MULTI_LINE_STYLE);
	}
}
